package com.java.system.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.system.common.constant.CommonConstants;
import com.java.system.common.dao.TKMapper;
import com.java.system.common.service.BaseService;
import com.java.system.common.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

public class BaseServiceImpl<M extends TKMapper<T>, T> implements BaseService<T> {
    @Autowired
    protected M dao;

    public M getDao() {
        return dao;
    }

    public void setDao(M dao) {
        this.dao = dao;
    }

    @Override
    public List<T> selectByExample(Example example) {
        return dao.selectByExample(example);
    }

    @Override
    public T selectOne(T entity) {
        return dao.selectOne(entity);
    }

    @Override
    public T selectByE(Example en) {
        return (T) dao.selectByExample(en);
    }

    @Override
    public T selectById(Object id) {
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public List<T> selectList(T entity) {
        return dao.select(entity);
    }

    @Override
    public PageInfo<T> page(T entity, Integer page, Integer size) {
        page = page == null ? 1 : page;
        size = size == null ? 10 : size;
        PageHelper.startPage(page, size);
        return new PageInfo<>(this.selectList(entity));
    }

    @Override
    public List<T> selectListAll() {
        return dao.selectAll();
    }

    @Override
    public Long selectCount(T entity) {
        return Long.valueOf(dao.selectCount(entity));
    }

    @Override
    public void insert(T entity) {
        EntityUtils.setCreatAndUpdatInfo(entity);
        dao.insert(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSelective(T entity) {
        EntityUtils.setCreatAndUpdatInfo(entity);
        dao.insertSelective(entity);
    }

    @Override
    public void delete(T entity) {
        dao.delete(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Object id) {
        dao.deleteByPrimaryKey(id);
    }

    @Override
    public void updateById(T entity) {
        EntityUtils.setUpdatedInfo(entity);
        dao.updateByPrimaryKey(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelectiveById(T entity) {
        EntityUtils.setUpdatedInfo(entity);
        dao.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void addOrEdit(T entity) {
        if (EntityUtils.isPKNotNull(entity, CommonConstants.PRIMARY_KEY)) {
            EntityUtils.setCreatAndUpdatInfo(entity);
        } else {
            EntityUtils.setUpdatedInfo(entity);
        }
        dao.addOrEdit(entity);
    }

    @Override
    public void addOrEditSelective(T entity) {
        if (EntityUtils.isPKNotNull(entity, CommonConstants.PRIMARY_KEY)) {
            EntityUtils.setCreatAndUpdatInfo(entity);
        } else {
            EntityUtils.setUpdatedInfo(entity);
        }
        dao.addOrEditSelective(entity);
    }

    @Override
    public void batchInsert(List<T> entitys) {
        entitys.forEach(entity -> {
            EntityUtils.setCreatAndUpdatInfo(entity);
        });
        dao.batchInsert(entitys);
    }

    @Override
    public void batchInsertOrUpdate(List<T> entitys) {
        entitys.forEach(entity -> {
            if (EntityUtils.isPKNotNull(entity, CommonConstants.PRIMARY_KEY)) {
                EntityUtils.setUpdatedInfo(entity);
            } else {
                EntityUtils.setCreatAndUpdatInfo(entity);
            }
            dao.batchInsertOrUpdate(entitys);
        });
    }

}
